export default {
	namespaced:true,
	state(){
		return {
			popShow:false,
			popData:null, //弹窗数据包
			cartList:[], //购物车列表
			editIdx:-1 //区分新增、编辑
		}
	},
	getters:{
		allInfo(state){
			let allCheck = true
			let total = 0
			state.cartList.forEach(item=>{
				if(!item.ischeck){
					allCheck = false
				}
				if(item.ischeck){
					let {list,dropIdx,num} = item
					total += list[dropIdx].price*num
				}
			})
			return {
				allCheck,
				total
			}
		}
	},
	mutations:{
		popShowMut(state,bool){
			state.popShow = bool
		},
		popDataMut(state,goodsData){
			state.popData = goodsData
		},
		cartAddMut(state,cartGood){
			let {objectId,dropIdx,num} = cartGood
			let {cartList,editIdx} = state
			//判断相同商品
			for(let i=0;i<cartList.length;i++){
				if(cartList[i].objectId==objectId&&cartList[i].dropIdx==dropIdx){
					state.cartList[i].num+=num
					if(editIdx!=-1){
						state.cartList.splice(editIdx,1)
					}
					return
				}
			}
			//编辑商品
			if(editIdx!=-1){
				cartList[editIdx] = cartGood
				state.cartList = [...cartList]
				return
			}
			//新增新商品
			cartGood.ischeck = true //控制勾选状态
			state.cartList.push(cartGood)
		},
		//单选
		cartCheckMut(state,idx){
			state.cartList[idx].ischeck = !state.cartList[idx].ischeck
		},
		//全选
		cartAllCheckMut(state,bool){
			state.cartList.forEach(item=>{
				item.ischeck = !bool
			})
		},
		//删除
		cartDelMut(state,idx){
			state.cartList.splice(idx,1)
		},
		//记录编辑下标
		editIdxMut(state,idx){
			state.editIdx = idx
		}
		
	}
}